package com.yoso.log.interfaces.controller.order;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.yoso.base.infrastructure.constants.ErrorMsg;
import com.yoso.base.interfaces.controller.BaseController;
import com.yoso.base.interfaces.controller.BaseResponse;
import com.yoso.base.service.BaseEntityExample;
import com.yoso.base.service.IBaseService;
import com.yoso.log.application.service.IOrderLogService;
import com.yoso.log.domain.order.OrderLogEntity;
import com.yoso.log.domain.order.OrderLogEntityExample;
import com.yoso.log.interfaces.vo.OrderLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author shizejin
 * @date 2022/2/7
 * @description:
 */
@RestController
@RequestMapping("/orderLog")
@Api(tags = "订单日志信息")
@Slf4j
public class OrderLogController extends BaseController<OrderLogVo, OrderLogEntity> {
    @Autowired
    private IOrderLogService orderLogService;

    @Override
    public OrderLogEntity V2E(OrderLogVo data) {
        return JSON.parseObject(JSON.toJSONString(data), OrderLogEntity.class);
    }

    @Override
    public OrderLogVo E2V(OrderLogEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), OrderLogVo.class);

    }

    @Override
    public IBaseService<OrderLogVo, ? extends BaseEntityExample> getService() {
        return this.orderLogService;
    }

    @ApiOperation("获取当前订单日志")
    @PostMapping("/getCurrentOrderLog")
    public BaseResponse order(@NotNull String orderCode) {
        OrderLogEntityExample example = new OrderLogEntityExample();
        example.setOrderByClause("order_time");
        example.createCriteria().andCodeEqualTo(orderCode);
        List<OrderLogVo> orderLogVos = this.orderLogService.selectByExample(example);
        if (CollUtil.isEmpty(orderLogVos)){
            return this.error("未查到当前订单的日志!!!");
        }
        log.info("当前订单日志:{}",JSON.toJSONString(orderLogVos));
        return this.success("获取成功！",orderLogVos);
    }
}
